<?php

namespace App\Admin\Controllers;

use App\Models\ApplicationForm;
use App\Models\Club;
use App\Models\School;
use App\Models\StudentApplication;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class StudentApplicationController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'StudentApplication';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $formId = $_GET['form_id'];
        $grid = new Grid(new StudentApplication());
        $grid->model()->where('form_id',$formId);
        $grid->column('id', __('Id'));
        $grid->column('club.club_name', __('社团班级'));
        $grid->column('school.school_name', __('学校'));
        $grid->column('class_and_grade', __('年级'))->display(function ($classAndGrade) {
            return "<span>{$classAndGrade['grade']} {$classAndGrade['class_name']}</span>";
        });
        $grid->column('school_class_name', __('班级'));
        $grid->column('class_and_grade_time', __('上课时间'))->display(function ($classAndGradeTime) {
            return "<span>{$classAndGradeTime['week_text']}:{$classAndGradeTime['start_time']}-{$classAndGradeTime['end_time']}</span>";
        });
        $grid->column('name', __('学生姓名'));
        $grid->column('phone', __('电话'));
        $grid->column('created_at', __('报名时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(StudentApplication::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('club_id', __('Club id'));
        $show->field('school_id', __('School id'));
        $show->field('class_and_grade_id', __('Class and grade id'));
        $show->field('time_id', __('Time id'));
        $show->field('name', __('Name'));
        $show->field('school_class_name', __('School Class Name'));
        $show->field('phone', __('Phone'));
        $show->field('price', __('Price'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('form_id', __('Form id'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new StudentApplication());

        $form->select('club_id', __('社团班级'))->options(Club::all()->pluck('club_name', 'id'))
            ->load('class_and_grade_id', '/api/get_class_and_grade_by_club');
        $form->select('school_id', __('学校'))->options(School::all()->pluck('school_name', 'id'))
            ->load('class_and_grade_id', '/api/get_class_and_grade_by_school');
        $form->select('class_and_grade_id', __('年级'))->load('time_id', '/api/get_class_and_grade_time');
        $form->select('time_id', __('时间段'));
        $form->text('name', __('学生姓名'));
        $form->text('school_class_name', __('班级'));
        $form->mobile('phone', __('电话'));
        $form->select('form_id', __('表单名称'))->options(ApplicationForm::all()->pluck('form_name', 'id'));

        return $form;
    }
}
